package com.lianwei.service.impl;

import com.lianwei.dao.UserMapper;
import com.lianwei.enums.SendCodeEnum;
import com.lianwei.enums.UserCodeEnum;
import com.lianwei.pojo.LoginUser;
import com.lianwei.pojo.User;
import com.lianwei.pojo.request.UserLoginRequest;
import com.lianwei.pojo.request.UserRegisterRequest;
import com.lianwei.service.NotifyService;
import com.lianwei.service.UserService;
import com.lianwei.util.CommonUtil;
import com.lianwei.util.JWTUtil;
import com.lianwei.util.JsonData;
import jodd.util.ObjectUtil;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    private NotifyService notifyService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    //将第一次查询所有的用户的集合提取出来
    List<User> userList;
    @Override
    public JsonData login(UserLoginRequest userLoginRequest) {
        if (userLoginRequest==null||StringUtil.isBlank(userLoginRequest.getPassword())||StringUtil.isBlank(userLoginRequest.getMail())){
            return JsonData.buildResult(UserCodeEnum.ACCOUNT_ILLEGAL);
        }
        User user = userMapper.selectUserByMail(userLoginRequest.getMail());
        if (user==null){
            return JsonData.buildResult(UserCodeEnum.ACCOUNT_UNREGISTER);
        }
        if (!CommonUtil.MD5(userLoginRequest.getPassword()).equals(user.getPassword())){
            return JsonData.buildResult(UserCodeEnum.ACCOUNT_PWD_ERROR);
        }
        LoginUser loginUser = new LoginUser();
        BeanUtils.copyProperties(user,loginUser);
        System.out.println(user);
        loginUser.setId(Long.parseLong(user.getId()+""));
        System.out.println(loginUser);
        String token = JWTUtil.geneJsonWebToken(loginUser);
         return JsonData.buildSuccess(token,"登录成功");
    }

    @Override
    public JsonData register(UserRegisterRequest userRegisterRequest) {
        boolean checkCode = false;
        if (StringUtil.isNotBlank(userRegisterRequest.getMail())){
            checkCode = notifyService.checkEmailCode(SendCodeEnum.USER_REGISTER,userRegisterRequest.getMail(),userRegisterRequest.getCaptcha());

        }
        if (!checkCode){
            return JsonData.buildResult(UserCodeEnum.CODE_ERROR);
        }
        if (!checkUnique(userRegisterRequest.getMail())){
           return JsonData.buildResult(UserCodeEnum.ACCOUNT_REPEAT);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegisterRequest,user);
        user.setPassword(CommonUtil.MD5(user.getPassword()));

        userMapper.register(user);
        return JsonData.buildSuccess("注册成功");
    }

    /**
     * 用户禁用
     * @param id
     */
    @Override
    public void disable(Integer id) {
        userMapper.disable(id);
    }

    /**
     * 用户启用
     * @param id
     */
    @Override
    public void enable(Integer id) {
        userMapper.enable(id);
    }

    @Override
    public List<User> queryList() {
        userList = userMapper.queryList();
        return userList;
    }

    @Override
    public List<User> queryListByName(String name) {
        userList = userMapper.queryList(name);
        return userList;
    }

    /**
     * @describe 账号唯一性检查
     * @param mail
     * @return boolean
     * @author kuiwen.sun
     */
    private boolean checkUnique(String mail){
        return userMapper.selectUserCountByMail(mail)<=0;
    }
}
